package login;
import bean.SendMassage;
import bean.User;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import httpclient.QuickStart;

//@WebServlet(name = "Login",urlPatterns = {"/Login","/login/dgut"})
@WebServlet(name = "Login",urlPatterns = {"login/dgut"})
public class Login extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        String token=request.getParameter("token");
        String appid="javaee";
        String appsecret = "b3b52e43ccfd";
        String userip = request.getLocalAddr();     //获取本地ip
        String url = "https://cas.dgut.edu.cn/ssoapi/v2/checkToken";
        List<NameValuePair> s = new ArrayList<>();
        s.add(new BasicNameValuePair("token",token));
        s.add(new BasicNameValuePair("appid",appid));
        s.add(new BasicNameValuePair("appsecret",appsecret));
        s.add(new BasicNameValuePair("userip",userip));
        String json = QuickStart.post(url,s);
        System.out.println(json + "111111111111111111111111111111111111111111111");

        SendMassage m= JSON.parseObject(json,SendMassage.class);
        //System.out.println(oid);
        if(m.getAccess_token()!=null){
            String access_token = m.getAccess_token();
            String openid = m.getOpenid();
            List<NameValuePair> s1 = new ArrayList<>();
            s1.add(new BasicNameValuePair("access_token",access_token));
            s1.add(new BasicNameValuePair("openid",openid));
            String json1=QuickStart.post("https://cas.dgut.edu.cn/oauth/getUserInfo",s1);
            Jsonb jsonb= JsonbBuilder.create();
            System.out.println(json1 + "222222222222222222222222222222222222222222222222222222");
            User user=jsonb.fromJson(json1,User.class);
            System.out.println(user.getName());
            if(user.getName()==null){
                System.out.println("3333333333333333333333333333333333333333333333333333333333");
                Jsonb jsonb1 = JsonbBuilder.create();
                String jsonPerson = jsonb1.toJson(json1);
                response.getWriter().write(jsonPerson);

            }
            else{
                System.out.println(user);
                System.out.println("4444444444444444444444444444444444444444444444444444444444444");
                request.getSession().setAttribute("user",user);
                response.sendRedirect("/index.jsp");

            }
        }


    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
